package com.picshare.controller;

import com.picshare.service.StreamListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import com.picshare.event.PhotoUploadedEvent;
import javax.servlet.http.HttpSession;

/**
 * SSE流媒体控制器
 * 提供实时照片上传监听功能
 */
@RestController
@RequestMapping("/api")
public class StreamController {
    
    @Autowired
    private StreamListenerService streamListenerService;
    
    /**
     * SSE流端点
     * 客户端通过此端点监听照片上传事件
     */
    @GetMapping("/stream")
    public SseEmitter stream(HttpSession session) {
        return streamListenerService.createEmitter(session);
    }
    
    /**
     * 监听照片上传事件
     */
    @EventListener
    @Async
    public void handlePhotoUploaded(PhotoUploadedEvent event) {
        streamListenerService.handlePhotoUploaded(event);
    }
}